package pub.guoxin.oops.manager.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pub.guoxin.oops.common.cache.CacheService;
import pub.guoxin.oops.common.sms.service.SmsFuckService;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.utils.RandomUtils;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import static pub.guoxin.oops.common.cache.CacheKeyFormatEnums.SMS_CAPTCHA;

/**
 * 在一次重构后运行时请求这个接口 SmsService 就tmd报 NullPointException 日了
 * 最关键这里的代码都没动，断断续续找了好几天原因，没办法这里的代码量也不是很大从写
 * 了一边，就tmd叫SmsFuck
 * <p>
 * Created by guoxin on 17-11-25.
 */
@RestController
@RequestMapping(OopsPathConstants.SMS_PATH)
public class SmsFuckController {

    @Autowired
    private ResultVoBuilder builder;
    @Autowired
    private SmsFuckService smsFuckService;
    @Autowired
    private CacheService cacheService;

    @ApiOperation(value = "发送手机验证码", notes = "发送手机验证码")
    @PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo add(String mobile) {
        String captcha = RandomUtils.random(6);
        // 发送短信验证码
        smsFuckService.send(mobile, captcha);
        // 缓存验证码
        cacheService.put(SMS_CAPTCHA.key(mobile), captcha, 120);
        return builder.buildSuccessResultInfo();
    }

}
